<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公司名或客户名" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入公司名或客户名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业务类型" prop="businessType">
        <el-select v-model="queryParams.businessType" placeholder="请选择业务类型" clearable size="small">
          <el-option
            v-for="dict in businessTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="联系人" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入联系人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="tel">
        <el-input
          v-model="queryParams.tel"
          placeholder="请输入联系方式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:customer:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:customer:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:customer:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:customer:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="customerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户名称" align="center" prop="customerName" />
      <el-table-column label="业务类型" align="center" prop="businessType" :formatter="businessTypeFormat" />
      <el-table-column label="联系人" align="center" prop="name" />
      <el-table-column label="联系方式" align="center" prop="tel" />
      <el-table-column label="所属律师" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:customer:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:customer:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-plus-outline"
            @click="handleAddCase(scope.row)"
            v-hasPermi="['business:case:add']"
          >新建案件</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="是否为系统用户" >
          <el-switch v-model="switchValue"
           active-color="#13ce66" 
           inactive-color="#ff4949"
           active-text="是"
            inactive-text="否"
            @change="switchChange"/>
        </el-form-item>
        <el-form-item label="公司名或客户名" prop="customerName" v-if="noIf">
          <el-input v-model="form.customerName" placeholder="请输入公司名或客户名" />
        </el-form-item>
        <el-form-item label="系统用户选择" v-if="yesIf" prop="customerName">
        <el-select prop="customerName" v-model="form.customerName" clearable size="small" >
        <el-option 
        v-for="dict in customerOptions"
                  :key="dict.userName"
                  :label="dict.userName"
                  :value="dict.userName"
        />
        </el-select>
      </el-form-item>
        <el-form-item label="业务类型" prop="businessType">
          <el-select v-model="form.businessType" placeholder="请选择业务类型">
            <el-option
              v-for="dict in businessTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系人" prop="name">
          <el-input v-model="form.name" placeholder="请输入联系人姓名" />
        </el-form-item>
        <el-form-item label="联系方式" prop="tel">
          <el-input v-model="form.tel" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 新建案件弹窗 -->
    <el-dialog title="新建案件" :visible.sync="caseOpen" width="500px" append-to-body>
      <el-form ref="forn" :model="forn" :rules="rules" label-width="80px">
        <el-form-item label="案件编号" prop="caseCode">
          <el-input v-model="forn.caseCode" placeholder="请输入案件编号" />
        </el-form-item>
        <el-form-item label="案件类型" prop="caseType">
          <el-select v-model="forn.caseType" placeholder="请选择案件类型">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="forn.remark" placeholder="请输入备注" type="textarea" :rows="3"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForn">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCustomer, getCustomer, delCustomer, addCustomer, updateCustomer, exportCustomer } from "@/api/business/customer";
import { addCase } from "@/api/business/case";
import { listUser } from "@/api/system/user";
import { getRole } from "@/api/business/book";
export default {
  name: "Customer",
  components: {
  },
  data() {
    return {
      customerQuery:{
        deptId:205,
      },
      switchValue:false,
      yesIf:false,
      noIf:false,
      customerOptions:[],
      // 案件类型字典
      typeOptions: [],
      caseOpen:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户表格数据
      customerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 业务类型字典
      businessTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerName: null,
        businessType: null,
        name: null,
        tel: null,
        createBy:null,
      },
      // 表单参数
      form: {},
      //新建案件表单参数
      forn:{
        customerId:null,
        caseCode:null,
        caseType:undefined,
        remark:null,
      },
      // 表单校验
      rules: {
        customerName: [
          { required: true, message: "公司名或客户名不能为空", trigger: "blur" }
        ],
        businessType: [
          { required: true, message: "业务类型不能为空", trigger: "change" }
        ],
        name: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        tel: [
          { required: true, message: "联系方式不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getCustomer();
    this.getList();
    this.getDicts("poppy_question_type").then(response => {
      this.businessTypeOptions = response.data;
      this.typeOptions = response.data;
    });
  },
  methods: {
    //滑块change事件
    switchChange(){
      this.yesIf = false;
      this.noIf = false;
      if(this.switchValue == true){
        this.yesIf = true;
      }else{
        this.noIf = true;
      }
    },
    //获取系统客户
    getCustomer(){
      listUser(this.customerQuery).then(response=>{
        console.log("返回的系统客户")
        console.log(response.rows)
        this.customerOptions = response.rows
      })
    },
    //为客户添加案件
    handleAddCase(row){
      this.forn.customerId = row.customerName;
      this.caseOpen = true;
    },
    /** 查询客户列表 */
    getList() {
      getRole().then(response=>{
        if(response.deptId == 108){
          this.queryParams.createBy = response.userName;
          
        }
        this.loading = true;
            listCustomer(this.queryParams).then(response => {
              this.customerList = response.rows;
              this.total = response.total;
              this.loading = false;
            });
      })
      
    },
    // 业务类型字典翻译
    businessTypeFormat(row, column) {
      return this.selectDictLabel(this.businessTypeOptions, row.businessType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.caseOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customerName: null,
        businessType: null,
        name: null,
        tel: null,
        createBy: null,
        createTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCustomer(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCustomer(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCustomer(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /**新增案件提交按钮 */
    submitForn(){ 
          addCase(this.forn).then(response=>{
            this.msgSuccess("新增案件成功");
            this.caseOpen = false;
          })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除客户编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCustomer(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有客户数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCustomer(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
